﻿using NHibernateDemo.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using NHibernate;
using NHibernateDemo.DAL.Entity;
using log4net;
using NUnit2 = NUnit.Framework;
using System.IO;
using System.Collections.Generic;
namespace NHibernateDemo.DAL.Test
{
    
    
    /// <summary>
    ///This is a test class for FundBasicDALTest and is intended
    ///to contain all FundBasicDALTest Unit Tests
    ///</summary>
    [TestClass( )]
    [NUnit2.TestFixture]
    public class FundBasicDALTest {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion
        [TestInitialize]
        public void Setup( ) {
            log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize( ); 
        }

        /// <summary>
        ///A test for FundBasicDAL Constructor
        ///</summary>
        [TestMethod( )]
        public void FundBasicDALConstructorTest( ) {
            ISession session = null; // TODO: Initialize to an appropriate value
            FundBasicDAL target = new FundBasicDAL();
            //Assert.Inconclusive("TODO: Implement code to verify target");
        }

        /// <summary>
        ///A test for CreateFund
        ///</summary>
        [TestMethod( )]
        [NUnit2.Test]
        public void Create_UpdateFundTest( ) {
            
            // TODO: Initialize to an appropriate value
            FundBasicDAL target = new FundBasicDAL(); // TODO: Initialize to an appropriate value
            var category = new Category { CategoryId = "$$123xxx$$", Name = "Category1" };
            FundBasic fund = new FundBasic {
                FundId = "$FO1237770",
                FundName = "Fund1",
                Category = category
            }; // TODO: Initialize to an appropriate value
            
            target.Save(fund);
            var newFund = target.GetById(fund.FundId);
            Assert.AreEqual(fund.FundId, newFund.FundId);
            Assert.AreEqual(fund.Category.Name, newFund.Category.Name);
            fund.FundName = "Fund2";
            fund.Category = new Category { CategoryId = "$$xxx123$$", Name = "Category2" };
            target.SaveOrUpdate(fund);
            newFund = target.GetById(fund.FundId);
            Assert.AreEqual(fund.FundId, newFund.FundId);
            Assert.AreEqual(fund.Category.Name, newFund.Category.Name);
            target.Delete(fund);
            var categoryDAL = new CategoryDAL( );
            categoryDAL.Delete(categoryDAL.GetById(fund.Category.CategoryId));
            categoryDAL.Delete(categoryDAL.GetById(category.CategoryId));
            //target.Delete(fund);
            //Assert.IsNull(target.GetById(fund.FundId));
            //Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod( )]
        public void DeleteTest( ) {
            ISession session = null; // TODO: Initialize to an appropriate value
            FundBasicDAL target = new FundBasicDAL(); // TODO: Initialize to an appropriate value
            var categoryDAL = new CategoryDAL( );

            FundBasic fund = new FundBasic {
                FundId = "$FO1237770",
                FundName = "Fund1",
                Category = new Category { CategoryId = "$$123xxx$$", Name = "Category1" }
            };
            target.Save(fund);
            //fund = target.GetById("OF123123xx");
            target.Delete(fund);
            FundBasic nFund = target.GetById("$FO1237770");
            Assert.IsNull(nFund);
            
            Assert.AreEqual("$$123xxx$$", categoryDAL.GetById("$$123xxx$$").CategoryId);
            categoryDAL.Delete(categoryDAL.GetById("$$123xxx$$"));
            //Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

       

        /// <summary>
        ///A test for GetById
        ///</summary>
        //[TestMethod( )]
        public void GetByIdTest( ) {
            ISession session = null; // TODO: Initialize to an appropriate value
            FundBasicDAL target = new FundBasicDAL(); // TODO: Initialize to an appropriate value
            string id = "123"; // TODO: Initialize to an appropriate value
            FundBasic expected = null; // TODO: Initialize to an appropriate value
            FundBasic actual;
            actual = target.GetById(id);
            Assert.AreEqual(1, actual.Managers.Count);

            PersonBasicDAL pDAL = new PersonBasicDAL( );
            var person = pDAL.GetById(80);
            Assert.AreEqual(1, person.ManageFunds.Count);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }

        [TestMethod( )]
        public void AddPersonManagement( ) {
            FundBasicDAL fundDAL = new FundBasicDAL( );
            PersonBasicDAL personDAL = new PersonBasicDAL( );
            var fund = new FundBasic { FundId = "123", FundName = "Fund123", Category = new Category { CategoryId = "c1", Name = "xxx" } };
            fundDAL.Delete(fund);
            

            var person = new PersonBasic { Age = 12, Name = new Name { FirstName = "g", LastName = "cc" } };
            personDAL.Delete(person);
            person.ManageFunds.Add(fund);
            fund.Managers.Add(person);

            personDAL.Save(person);
            //fundDAL.CreateFund(fund);
            

            var nPerson = personDAL.GetById(person.PersonId);
            Assert.AreEqual(1, nPerson.ManageFunds.Count);
            var nFund = fundDAL.GetById(fund.FundId);
            Assert.AreEqual(1, nFund.Managers.Count);
            fund.Managers.Remove(person);
            nFund.Managers.Remove(person);
            //fundDAL.Delete(nFund);
            personDAL.Delete(nPerson);
        }





    }
}
